﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SmartMathLibrary.LinearEquationSolvers.Preconditioners
{
    /// <summary>
    /// This class represents a SOR preconditioner.
    /// </summary>
    [Serializable]
    public class SORPreconditioner : IPreconditioner
    {
        /// <summary>
        /// The omega parameter of the preconditioner.
        /// </summary>
        private double w;

        /// <summary>
        /// The source matrix of the preconditioner.
        /// </summary>
        private SquareMatrix sourceMatrix;

        /// <summary>
        /// Initializes a new instance of the <see cref="SORPreconditioner"/> class.
        /// </summary>
        /// <param name="sourceMatrix">The source matrix for the precondition process.</param>
        public SORPreconditioner(SquareMatrix sourceMatrix)
        {
            this.w = 1;
            this.sourceMatrix = sourceMatrix;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="SORPreconditioner"/> class.
        /// </summary>
        /// <param name="sourceMatrix">The source matrix for the precondition process.</param>
        /// <param name="w">The omega parameter for the precondition process.</param>
        public SORPreconditioner(SquareMatrix sourceMatrix, double w)
        {
            if (sourceMatrix == (SquareMatrix) null)
            {
                throw new ArgumentNullException("sourceMatrix");
            }

            this.w = w;
            this.sourceMatrix = sourceMatrix;
        }

        /// <summary>
        /// Gets or sets the omega parameter for the precondition process.
        /// </summary>
        /// <value>The omega parameter for the precondition process.</value>
        public double W
        {
            get { return w; }
            set { w = value; }
        }

        /// <summary>
        /// Gets or sets the source matrix for the precondition process.
        /// </summary>
        /// <value>The source matrix for the precondition process.</value>
        public SquareMatrix SourceMatrix
        {
            get { return sourceMatrix; }
            set { sourceMatrix = value; }
        }

        /// <summary>
        /// Computes the precondition matrix of the specified matrix.
        /// </summary>
        /// <returns>
        /// The computed precondition matrix of the original matrix.
        /// </returns>
        public Matrix ComputePreconditionMatrix()
        {
            SquareMatrix diagonal = new SquareMatrix(this.sourceMatrix.Rows);
            SquareMatrix inverseDiagonal = new SquareMatrix(this.sourceMatrix.Rows);
            SquareMatrix lower = new SquareMatrix(this.sourceMatrix.Rows);
            SquareMatrix upper = new SquareMatrix(this.sourceMatrix.Rows);

            for (int i = 0; i < this.sourceMatrix.Rows; i++)
            {
                for (int j = 0; j < this.sourceMatrix.Rows; j++)
                {
                    if (i > j)
                    {
                        lower.SetValueAtPosition(i, j, this.sourceMatrix.GetValueAtPosition(i, j));
                    }
                    else if (i == j)
                    {
                        diagonal.SetValueAtPosition(i, j, this.sourceMatrix.GetValueAtPosition(i, j));
                        inverseDiagonal.SetValueAtPosition(i, j, 1/this.sourceMatrix.GetValueAtPosition(i, j));
                    }
                    else
                    {
                        upper.SetValueAtPosition(i, j, this.sourceMatrix.GetValueAtPosition(i, j));
                    }
                }
            }

            return (diagonal*(1/w) + lower)*inverseDiagonal*(w/(2 - w))*(diagonal*(1/w) + upper);
        }
    }
}